package chapter01;

/**
 * 测试串行和并发执行时间
 */
public class ConcurrencyTest {
    private static final long count=10000L;
    public static void main(String[] args) throws InterruptedException {
       concurrency();
       serial();
    }

    /**
     * 并发执行
     * @throws InterruptedException
     */
    private static void concurrency() throws InterruptedException {
        Long start=System.currentTimeMillis();
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                long a=0;
                for(long i=0;i<count;i++){
                    a+=5;
                }

            }
        });

        thread.start();
        long b=0;
        for(long i=0;i<count;i++){
            b--;
        }
        long time=System.currentTimeMillis()-start;
        thread.join();
        System.out.println("concurrent: "+time+"ms,b="+b);
    }

    /**
     * 串行
     */
    private static void serial(){
        Long start=System.currentTimeMillis();
        long a=0;
        for(long i=0;i<count;i++){
            a+=5;
        }
        long b=0;
        for(long i=0;i<count;i++){
            b--;
        }
        long time=System.currentTimeMillis()-start;
        System.out.println("serial: "+time+"ms,b="+b+",a="+a);
    }
}
